package com.mouse.magent.common.entity.common;


import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.mouse.magent.common.exception.BaseException;
import com.mouse.magent.common.exception.ErrorType;
import com.mouse.magent.common.exception.SystemErrorType;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;

import java.time.Instant;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@ApiModel(description = "返回模型")
@Getter
public class Result<T> {


    @ApiModelProperty(value = "状态", required = true)
    private String code;
    @ApiModelProperty(value = "描述")
    private String message;
    @ApiModelProperty(value = "结果")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private T data;

    public Result() {

    }

    public Result(ErrorType errorType) {
        this.code = errorType.getCode();
        this.message = errorType.getMessage();
    }

    public Result(ErrorType errorType, T data) {
        this(errorType);
        this.data = data;
    }

    public Result(String code, String message) {
        this.code = code;
        this.message = message;
    }


    private Result(String code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static <T> Result<T> success(T data) {
        return new Result<T>(SystemErrorType.SYSTEM_SUCCESS, data);
    }

    public static Result success() {
        return success(null);
    }

    public static Result fail() {
        return new Result(SystemErrorType.SYSTEM_ERROR);
    }

    public static Result fail(BaseException baseException) {
        return fail(baseException, null);
    }

    public static Result systemError(String massage) {
        SystemErrorType errorType = SystemErrorType.SYSTEM_ERROR;
        errorType.setMessage(massage);
        return fail(new BaseException(errorType, null));
    }


    public static Result fail(BaseException baseException, Object data) {
        return new Result<>(baseException.getErrorType(), data);
    }

    public static Result fail(ErrorType errorType, Object data) {
        return new Result<>(errorType, data);
    }

    public static Result fail(ErrorType errorType) {
        return Result.fail(errorType, null);
    }

    public static Result fail(Object data) {
        return new Result<>(SystemErrorType.SYSTEM_ERROR, data);
    }


    /**
     * 成功code=000000
     *
     * @return true/false
     */
    @JsonIgnore
    public boolean isSuccess() {
        return SystemErrorType.SYSTEM_SUCCESS.getCode().equals(this.code);
    }

    /**
     * 失败
     *
     * @return true/false
     */
    @JsonIgnore
    public boolean isFail() {
        return !isSuccess();
    }
}
